import { Decimal } from 'decimal.js'
import { createGlobalState, useStorage } from '@vueuse/core'

let calculates = {
    toFixed(value, precision = 2) {
        return new Decimal(value).toFixed(precision)
    },
    add(...values) {
        let result = 0
        values.forEach(value => {
            if (isNaN(value)) {
                return
            }
            result = new Decimal(value).add(result)
        })
        return result.toNumber()
    },
    sub(...values) {
        let result = 0
        values.forEach((value, i) => {
            if (isNaN(value)) {
                return
            }
            if (i === 0) {
                result = new Decimal(value)
            } else {
                result = new Decimal(result).sub(value)
            }
        })
        return result.toNumber()
    },
    mul(...values) {
        let result = 0
        values.forEach((value, i) => {
            if (isNaN(value)) {
                return
            }
            if (i === 0) {
                result = new Decimal(value)
            } else {
                result = new Decimal(value).mul(result)
            }
        })
        return result.toNumber()
    },
    div(...values) {
        let result = 0
        values.forEach((value, i) => {
            if (isNaN(value)) {
                return
            }
            if (i === 0) {
                result = new Decimal(value)
            } else {
                if (!Decimal(value).isZero()) {
                    result = new Decimal(result).div(value)
                }
            }
        })
        return result.toNumber()
    },
    testVueUse: createGlobalState(() => {
        return useStorage('vue-use-locale-storage', {
            name: 'Banana',
            color: 'Yellow',
            size: 'Medium',
        })
    })
    
}

export default calculates

